import db from "../models/index.js";

let gupiaoList = db.gupiao_list;
let gupiaoListDao = {};


// 获取表中的所有股票
gupiaoListDao.get = () => {
  return gupiaoList.findAll({
    raw:true
  });
};

// 增加股票 gupiao_name, gupiao_price 这两个可以为undefined,这时候不更新数据即可
gupiaoListDao.add = async (gupiao_id, gupiao_name, gupiao_price, t) => {
  if (!t) {
    await gupiaoList.findOrCreate({where: {gupiao_id}, transaction: t});
    return true;
  }
  await gupiaoList.findOrCreate({where: {gupiao_id}, transaction: t});
  return true;
};

// 删除股票
gupiaoListDao.delete = async (gupiao_id, t) => {
  if (!t) {
    return gupiaoList.destroy({where: {gupiao_id}});
  }
  return gupiaoList.destroy({where: {gupiao_id}, transaction: t});
};

// 更新股票
gupiaoListDao.update = async (gupiao_id, gupiao_name, gupiao_price, t) => {
  if(!t) {
    return gupiaoList.update({gupiao_name, gupiao_price}, {where: {gupiao_id}});
  }
  return  gupiaoList.update({gupiao_name, gupiao_price}, {where: {gupiao_id}, transaction: t});
};
export default gupiaoListDao;


